package com.yjck.jwt.Interceptor;


import com.yjck.jwt.config.JwtConstants;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @author EDZ
 * 封装映射 注解loginUser 对象信息
 */
@Component
public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

/*    @Autowired
    private ISysUserService sysUserService;*/
/*    @Autowired
    private AppUserService appUserService;*/

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
       /* // 关联 实体类跟loginUser注解
        return (parameter.getParameterType().isAssignableFrom(SysUser.class) ||
                parameter.getParameterType().isAssignableFrom(AppUser.class)) &&
                parameter.hasParameterAnnotation(LoginUser.class);*/
        return true;
    }
/*    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        // 关联 实体类跟loginUser注解
        return (parameter.getParameterType().isAssignableFrom(SysUser.class)) &&
                parameter.hasParameterAnnotation(LoginUser.class);
    }*/

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
                                  NativeWebRequest request, WebDataBinderFactory factory) throws Exception {
        //获取用户ID
        String userId = (String) request.getAttribute(JwtConstants.USER_ID_HEADER, RequestAttributes.SCOPE_REQUEST);
//        String type = (String) request.getAttribute(JwtConstants.USER_TYPE_HEADER, RequestAttributes.SCOPE_REQUEST);
        if (StringUtils.isEmpty(userId)) {
            return null;
        }
//        SysUser user = sysUserService.getById(userId);
//        return user;
        //获取用户信息
      /*  if (JwtConstants.APP.equals(type)) {
            SysUser user = sysUserService.getById(userId);
            return user;
        } else {
            AppUser appUser = appUserService.getById(userId);
            return appUser;
        }*/
        return null;
    }
}
